package com.taoyibao.hongcanting.ui.mine.delegate;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.ui.home.views.SecondaryItemCartArea;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * @author TangAnna
 * @description: 待支付
 * @date :${DATA} 14:32
 */
public class WaitPayDelegate extends BackToolbarDelegate {
    /**
     * 去结账
     */
    private TextView mTvNext;
    /**
     * 继续点单
     */
    private TextView mTvContinueAdd;
    private TextView mTvWay;
    private TextView mTvNub;
    private RecyclerView mRecyclerView;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_wait_pay;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("待支付");
        mTvNext = get(R.id.tv_includShoppingCart_next);
        mTvWay = get(R.id.tv_waitPay_way);
        mTvNub = get(R.id.tv_waitPay_nub);
        mTvContinueAdd = get(R.id.tv_includShoppingCart_continueAdd);
        mTvNext.setText(CodeUtils.getIdString(R.string.string_pay));
        mRecyclerView = get(R.id.rv_waitPay_list);

    }

    /**
     * 设置列表数据
     *
     * @param data
     */
    public void setListData(List<DishModel> data) {

        if (data == null || data.size() < 1) {
            return;
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new CommonAdapter<DishModel>(getActivity(), R.layout.item_shopping_cart_secondary_shell, data) {
            @Override
            protected void convert(ViewHolder holder, DishModel dishModel, int position) {
                SecondaryItemCartArea secondaryItemCartArea = (SecondaryItemCartArea) holder.getConvertView();
                secondaryItemCartArea.setData(dishModel);
                secondaryItemCartArea.isShowCountController(false);
            }
        });
    }

    /**
     * 设置数据UI
     */
    public void setData(OrderInfoModel data) {
        if (data == null) {
            setEmptyView(true);
            return;
        }
        setEmptyView(false);
        setSeatUI(data);
        if (!TextUtils.isEmpty(data.order_id)) {
            ((TextView) get(R.id.tv_waitPay_orderId)).setText(data.order_id);
        }
        //备注  去除赠送的加菜信息
//        if (!TextUtils.isEmpty(data.order_remark) && !TextUtils.equals(data.order_remark, "0")) {
//            String[] split = data.order_remark.split(CodeConstant.ZENG_SONG);
//            if (split != null && split.length > 0) {
//                if (!TextUtils.isEmpty(split[0]) && !TextUtils.equals(split[0], "0")) {
//                    ((TextView) get(R.id.tv_waitPay_remark)).setText(split[0]);
//                }
//            } else {
//                ((TextView) get(R.id.tv_waitPay_remark)).setText(data.order_remark);
//            }
//        }
        //备注
        if (!TextUtils.isEmpty(data.order_remark)) {
            String remark = data.order_remark;
            if (remark.contains(CodeConstant.ZENG_SONG)) {//判断包不包括赠送的标记符
                String[] split = remark.split(CodeConstant.ZENG_SONG);
                remark = split[0];
            }
            if (!TextUtils.equals(remark, "0") && !TextUtils.equals(remark, "00")) {
                ((TextView) get(R.id.tv_waitPay_remark)).setText(remark);
            }
        }

        //总价
        ((TextView) get(R.id.tv_totalMoney)).setText("总价:" + CodeUtils.formatAmountUnit(data.order_totalmoney));
        //已优惠
        ((TextView) get(R.id.tv_couponMoney)).setText("已优惠:-" + CodeUtils.formatAmountUnit(data.dish_couponmoney));
        //下单时间
        if (!TextUtils.isEmpty(data.order_paytime)) {
            ((TextView) get(R.id.tv_waitPay_time)).setText(data.order_addtime);
        }

        //餐厅名称
        if (!TextUtils.isEmpty(data.order_companyname)) {
            ((TextView) get(R.id.tv_waitPay_companyname)).setText(data.order_companyname);
        } else {
            ((TextView) get(R.id.tv_waitPay_companyname)).setText(CodeUtils.getIdString(R.string.string_restaurantName));
        }
    }


    /**
     * 根据就餐方式设置UI
     */
    public void setSeatUI(OrderInfoModel data) {
        if (!TextUtils.isEmpty(data.order_deskid)) {//堂食
            mTvContinueAdd.setVisibility(View.VISIBLE);
            mTvWay.setText(CodeUtils.getIdString(R.string.string_zhuoWei));
            mTvNub.setText(data.order_deskid);
        } else {//外带
            mTvContinueAdd.setVisibility(View.GONE);
            mTvWay.setText(CodeUtils.getIdString(R.string.string_jiuCanWay));
            mTvNub.setText(CodeUtils.getIdString(R.string.string_waiDai));
        }
    }

    /**
     * 设置空页面
     *
     * @param b true 显示空页面  false显示数据页面
     */
    public void setEmptyView(boolean b) {
        get(R.id.layout_waitPay_empty).setVisibility(b ? View.VISIBLE : View.GONE);
        get(R.id.layout_waitPay_data).setVisibility(b ? View.GONE : View.VISIBLE);
        get(R.id.layout_bottom).setVisibility(b ? View.GONE : View.VISIBLE);
    }
}
